package com.tencent.bugly.beta.upgrade;

import android.text.TextUtils;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.download.BetaReceiver;
import com.tencent.bugly.beta.download.DownloadListener;
import com.tencent.bugly.beta.download.DownloadTask;
import com.tencent.bugly.beta.global.d;
import com.tencent.bugly.beta.global.e;
import com.tencent.bugly.beta.global.f;
import com.tencent.bugly.beta.ui.h;
import com.tencent.bugly.proguard.ah;
import com.tencent.bugly.proguard.an;
import com.tencent.bugly.proguard.aq;
import com.tencent.bugly.proguard.bk;
import com.tencent.bugly.proguard.m;
import com.tencent.bugly.proguard.p;
import com.tencent.bugly.proguard.w;
import com.tencent.bugly.proguard.y;
import com.tencent.bugly.proguard.z;
import java.io.File;
import java.util.HashMap;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class c {
    public static c a = new c();
    public BetaGrayStrategy b;
    public DownloadTask c;
    public DownloadListener d;
    public UpgradeListener e;
    public UpgradeStateListener f;
    public boolean g;
    public boolean h;
    public d i;
    public d j;
    public int k;
    private final Object l = new Object();
    private final Object m = new Object();
    private DownloadListener n = new com.tencent.bugly.beta.download.a(3, this, 0);
    private a o = null;
    private d p;
    private boolean q;

    public BetaGrayStrategy a(y yVar) {
        BetaGrayStrategy betaGrayStrategy;
        BetaGrayStrategy betaGrayStrategy2;
        File[] listFiles;
        y yVar2 = yVar;
        synchronized (this.l) {
            BetaGrayStrategy betaGrayStrategy3 = (BetaGrayStrategy) com.tencent.bugly.beta.global.a.a("st.bch", BetaGrayStrategy.CREATOR);
            betaGrayStrategy = null;
            if (betaGrayStrategy3 != null && betaGrayStrategy3.a == null) {
                com.tencent.bugly.beta.global.a.a("st.bch");
                betaGrayStrategy3 = null;
            }
            if (betaGrayStrategy3 != null && betaGrayStrategy3.a != null && (betaGrayStrategy3.a.e.c <= e.E.w || betaGrayStrategy3.a.n != 1 || ((betaGrayStrategy3.a.f != null && TextUtils.equals(e.E.v, betaGrayStrategy3.a.f.a)) || betaGrayStrategy3.a.p == 3))) {
                com.tencent.bugly.beta.global.a.a("st.bch");
                this.c = null;
                betaGrayStrategy3 = null;
            }
            if (yVar2 != null && yVar2.e.c < e.E.w) {
                an.a("versionCode is too small, discard remote strategy: [new: %d] [current: %d]", Integer.valueOf(yVar2.e.c), Integer.valueOf(e.E.w));
                yVar2 = null;
            }
            if (yVar2 != null) {
                if (yVar2.n == 2 && betaGrayStrategy3 != null && betaGrayStrategy3.a != null && !TextUtils.isEmpty(yVar2.m) && !TextUtils.isEmpty(betaGrayStrategy3.a.m) && TextUtils.equals(yVar2.m, betaGrayStrategy3.a.m)) {
                    an.a("callback strategy: %s", yVar2.m);
                    com.tencent.bugly.beta.global.a.a("st.bch");
                    e.E.p.a(betaGrayStrategy3.a.f.b, e.E.t.getAbsolutePath(), null, null).delete(true);
                    betaGrayStrategy3 = null;
                }
                if (yVar2.n != 1) {
                    an.a("invalid strategy: %s", yVar2.m);
                    yVar2 = null;
                }
            }
            if (yVar2 != null) {
                if (betaGrayStrategy3 != null && betaGrayStrategy3.a != null && !TextUtils.isEmpty(yVar2.m) && !TextUtils.isEmpty(betaGrayStrategy3.a.m) && TextUtils.equals(yVar2.m, betaGrayStrategy3.a.m)) {
                    betaGrayStrategy2 = new BetaGrayStrategy(aq.d(aq.a(betaGrayStrategy3)));
                    an.a("same strategyId:[new: %s] [current: %s] keep has popup times: %d, interval: %d", yVar2.m, betaGrayStrategy3.a.m, Integer.valueOf(betaGrayStrategy2.b), Long.valueOf(yVar2.i));
                } else {
                    betaGrayStrategy2 = new BetaGrayStrategy();
                }
                betaGrayStrategy2.a = yVar2;
                betaGrayStrategy2.e = System.currentTimeMillis();
                if (betaGrayStrategy3 != null) {
                    if (!betaGrayStrategy3.a.f.b.equals(yVar2.f.b)) {
                        if (this.c == null) {
                            e.E.p.a(betaGrayStrategy3.a.f.b, e.E.t.getAbsolutePath(), null, null).delete(true);
                            for (File file : e.E.t.listFiles()) {
                                if (!file.delete()) {
                                    an.e("cannot deleteCache file:%s", file.getAbsolutePath());
                                }
                            }
                        } else {
                            BetaReceiver.netListeners.remove(this.c.getDownloadUrl());
                            this.c.delete(true);
                            this.c = null;
                        }
                    }
                    if (betaGrayStrategy3.a.p == 3) {
                        File file2 = e.E.H;
                        if (file2 != null && file2.exists() && file2.delete()) {
                            an.a("delete tmpPatchFile", new Object[0]);
                        }
                        File file3 = e.E.G;
                        if (file3 != null && file3.exists() && file3.delete()) {
                            e.E.L = "";
                            an.a("delete patchFile", new Object[0]);
                        }
                    }
                }
                com.tencent.bugly.beta.global.a.a("st.bch", betaGrayStrategy2);
                an.a("onUpgradeReceived: %s [type: %d]", yVar2, Integer.valueOf(yVar2.g));
                p.a.a(new w("rcv", System.currentTimeMillis(), (byte) 0, 0L, yVar2.e, yVar2.m, yVar2.p, null));
                betaGrayStrategy = betaGrayStrategy2;
            } else if (betaGrayStrategy3 != null && betaGrayStrategy3.a != null && betaGrayStrategy3.a.p != 3) {
                com.tencent.bugly.beta.global.a.a("st.bch");
            }
        }
        return betaGrayStrategy;
    }

    public void a(boolean z, boolean z2, int i, y yVar, String str) {
        synchronized (this.l) {
            this.b = a(yVar);
            this.g = z;
            this.h = z2;
            if (this.e != null) {
                an.a("你已放弃让SDK来处理策略", new Object[0]);
                this.k = 3;
                if (this.b == null) {
                    an.a("betaStrategy is null", new Object[0]);
                }
                if (this.b != null && this.c == null) {
                    this.c = e.E.p.a(this.b.a.f.b, e.E.t.getAbsolutePath(), null, this.b.a.f.a);
                }
                if (this.c == null) {
                    an.a("用户自定义activity，创建task失败 [strategy:%s]", this.b);
                    this.b = null;
                    com.tencent.bugly.beta.global.a.a("st.bch");
                } else {
                    this.c.addListener(this.n);
                }
                com.tencent.bugly.beta.utils.e.a(new d(16, this.e, Integer.valueOf(i), this.b, Boolean.valueOf(z), Boolean.valueOf(z2)));
            }
            if (i != 0 && z && !z2 && this.b == null) {
                if (this.f != null) {
                    com.tencent.bugly.beta.utils.e.a(new d(18, this.f, -1, Boolean.valueOf(z)));
                } else {
                    com.tencent.bugly.beta.utils.e.a(new d(5, Beta.strToastCheckUpgradeError));
                }
                return;
            }
            if (this.b != null && this.b.a != null) {
                if (this.c == null) {
                    this.c = b();
                }
                this.c.addListener(this.n);
                if (this.f != null) {
                    com.tencent.bugly.beta.utils.e.a(new d(18, this.f, 0, Boolean.valueOf(z)));
                }
                if (this.e != null) {
                    return;
                }
                if (!z && this.b.a.g != 2) {
                    if (this.b.d && e.E.e) {
                        if ((com.tencent.bugly.beta.global.a.a(e.E.s) == 1 && e.E.S) || (com.tencent.bugly.beta.global.a.a(e.E.s) == 4 && e.E.T)) {
                            c();
                            return;
                        }
                        if (!z2) {
                            a(z);
                        }
                        return;
                    } else if (this.b.c + this.b.a.i > System.currentTimeMillis() || this.b.a.h - this.b.b <= 0 || this.b.a.g == 3) {
                        return;
                    }
                }
                if (((com.tencent.bugly.beta.global.a.a(e.E.s) == 1 && e.E.S) || (com.tencent.bugly.beta.global.a.a(e.E.s) == 4 && e.E.T)) && !z) {
                    c();
                } else if (!z2) {
                    a(z);
                }
            } else if (this.f != null) {
                com.tencent.bugly.beta.utils.e.a(new d(18, this.f, 1, Boolean.valueOf(z)));
            } else if (z && !z2) {
                com.tencent.bugly.beta.utils.e.a(new d(5, Beta.strToastYourAreTheLatestVersion));
            }
        }
    }

    private y a() {
        BetaGrayStrategy betaGrayStrategy = this.b;
        if (betaGrayStrategy == null) {
            return null;
        }
        return betaGrayStrategy.a;
    }

    private DownloadTask b() {
        y a2 = a();
        if (a2 == null) {
            return null;
        }
        if (this.c == null) {
            this.c = e.E.p.a(a2.f.b, e.E.t.getAbsolutePath(), null, this.b.a.f.a);
        }
        return this.c;
    }

    private void c() {
        y a2 = a();
        if (a2 == null) {
            return;
        }
        if (this.c == null) {
            this.c = b();
        }
        if (this.c == null) {
            return;
        }
        com.tencent.bugly.beta.global.a.a("st.bch", this.b);
        BetaReceiver.addTask(this.c);
        if (this.c.getStatus() == 1 && !this.h) {
            if (this.g && com.tencent.bugly.beta.global.a.a(e.E.s, this.c.getSaveFile(), a2.f.a)) {
                p.a.a(new w("install", System.currentTimeMillis(), (byte) 0, 0L, a2.e, a2.m, a2.p, null));
                return;
            } else {
                a(this.g);
                return;
            }
        }
        this.c.download();
    }

    private void a(boolean z) {
        y a2 = a();
        if (a2 == null) {
            return;
        }
        boolean z2 = false;
        if (System.currentTimeMillis() <= a2.a() - 86400000) {
            an.e(System.currentTimeMillis() + "ms", new Object[0]);
            return;
        }
        f.a.a(e.E.p, a2.l);
        if (this.c == null) {
            this.c = b();
        }
        DownloadTask downloadTask = this.c;
        if (downloadTask == null) {
            return;
        }
        if (!z && downloadTask.getStatus() == 2) {
            an.a("Task is downloading %s %s", a2.m, this.c.getDownloadUrl());
            return;
        }
        this.c.addListener(this.n);
        DownloadListener downloadListener = this.d;
        if (downloadListener != null) {
            this.c.addListener(downloadListener);
        }
        h hVar = h.v;
        hVar.a(a2, this.c);
        hVar.r = new d(3, this.b, this.c);
        hVar.s = new d(4, this.b, this.c, Boolean.valueOf(z));
        this.b.c = System.currentTimeMillis();
        com.tencent.bugly.beta.global.a.a("st.bch", this.b);
        if (z) {
            f.a.a(new d(2, hVar, Boolean.valueOf(z)), 3000);
            return;
        }
        f fVar = f.a;
        Object[] objArr = new Object[2];
        objArr[0] = hVar;
        objArr[1] = Boolean.valueOf((z || a2.g == 2) ? true : true);
        fVar.a(new d(2, objArr));
    }

    public void a(boolean z, boolean z2, int i) {
        synchronized (this.m) {
            BetaGrayStrategy betaGrayStrategy = (BetaGrayStrategy) com.tencent.bugly.beta.global.a.a("st.bch", BetaGrayStrategy.CREATOR);
            if (z && betaGrayStrategy != null && betaGrayStrategy.a != null && betaGrayStrategy.a.p == 3) {
                betaGrayStrategy = null;
            }
            BetaGrayStrategy betaGrayStrategy2 = betaGrayStrategy;
            if (this.o != null && !this.o.d && this.q == z) {
                synchronized (this.o) {
                    this.o.c[0] = Boolean.valueOf(z);
                    this.o.c[1] = Boolean.valueOf(z2);
                }
                this.p.b[0] = false;
                if ((z && !z2) || (betaGrayStrategy2 != null && betaGrayStrategy2.a != null && betaGrayStrategy2.a.p == 2)) {
                    com.tencent.bugly.beta.utils.e.b(this.p);
                    com.tencent.bugly.beta.utils.e.a(this.p, 6000L);
                }
            }
            this.q = z;
            if (this.o != null) {
                this.o.d = true;
            }
            this.o = new a(1, 804, Boolean.valueOf(z), Boolean.valueOf(z2), betaGrayStrategy2);
            this.p = new d(12, false, this.o);
            String str = "";
            long j = 0;
            if (betaGrayStrategy2 != null && betaGrayStrategy2.a != null) {
                str = betaGrayStrategy2.a.m;
                j = betaGrayStrategy2.a.o;
            }
            String str2 = str;
            long j2 = j;
            HashMap hashMap = new HashMap();
            hashMap.put("G16", e.E.L);
            b.a.a(804, ah.a((m) new z(z ? 1 : 0, str2, j2, hashMap)), this.o, z, e.E.F.a.e);
            if (z) {
                com.tencent.bugly.beta.utils.e.b(this.p);
                com.tencent.bugly.beta.utils.e.a(this.p, 6000L);
            }
            com.tencent.bugly.beta.utils.e.b(this.p);
            com.tencent.bugly.beta.utils.e.a(this.p, 6000L);
        }
    }

    public void a(bk bkVar) {
        if (e.E.F == null) {
            e.E.F = new BetaUploadStrategy();
        }
        if (bkVar == null || e.E.F.b == bkVar.h) {
            return;
        }
        e.E.F.b = bkVar.h;
        e.E.F.a.b = bkVar.b;
        e.E.F.a.c = bkVar.c;
        e.E.F.a.h = bkVar.h;
        if (aq.c(bkVar.d)) {
            e.E.F.a.d = bkVar.d;
        }
        if (aq.c(bkVar.e)) {
            e.E.F.a.e = bkVar.e;
        }
        if (bkVar.f != null && !TextUtils.isEmpty(bkVar.f.a)) {
            e.E.F.a.f.a = bkVar.f.a;
        }
        if (bkVar.g != null && bkVar.g.size() > 0) {
            e.E.F.a.g = bkVar.g;
        }
        if (aq.c(bkVar.i)) {
            e.E.F.a.i = bkVar.i;
        }
        if (aq.c(bkVar.j)) {
            e.E.F.a.j = bkVar.j;
        }
        com.tencent.bugly.beta.global.a.a("us.bch", e.E.F);
    }
}
